package com.cskaoyan.market.controller.admin;

import com.cskaoyan.market.db.domain.*;
import com.cskaoyan.market.service.admin.MarketUserManagerService;
import com.cskaoyan.market.util.ResponseUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

/**
 * @MethodName AdminUserManagerController
 * @Description 对应用户管理模块下搜索历史记录和意见反馈的内容
 * @Author M
 * @Date 15:02 2023/5/6
 **/
//@WebServlet(urlPatterns = {"/admin/history/list", "/admin/feedback/list" , "/admin/address/list", "/admin/collect/list", "/admin/footprint/list"})

@RequiresAuthentication
@RestController
@RequestMapping("admin")
public class AdminUserManagerController {

    @Autowired
    MarketUserManagerService adminUserManagerService;

    // /admin/user/list? page=1&limit=20&username=1&mobile=3&userId=&sort=add_time&order=desc
    @GetMapping("user/list")
    public Object userList(String page, String limit, String username, String mobile, String userId, String sort, String order) {
        if (StringUtils.isEmpty(sort)) {
            sort = "add_time";
        }
        if (StringUtils.isEmpty(order)) {
            order = "desc";
        }
        // 对page与limit进行校验，不能为非数字
        Integer pageInt = null;
        Integer limitInt = null;
        try {
            pageInt = Integer.parseInt(page);
            limitInt = Integer.parseInt(limit);
        } catch (Exception e) {
            // 发生错误，返回
            return ResponseUtil.badArgument();
        }
        List<MarketUser> marketUsers = adminUserManagerService.getUserList(pageInt, limitInt, userId, username, mobile, sort, order);
        Object o = ResponseUtil.okList(marketUsers);
        // 返回请求
        return o;
    }

    // http://localhost:8083/admin/user/detail?id=5
    @GetMapping("/user/detail")
    public Object searchUserById(Integer id){
        if (id == null){
            return ResponseUtil.badArgument();
        }
        MarketUser marketUser = adminUserManagerService.getUserById(id);
        Object o = ResponseUtil.ok(marketUser);
        // 返回请求
        return o;
    }

    // /admin/footprint/list
    @GetMapping("footprint/list")
    public Object footprintList(String page, String limit, String goodsId, String userId, String sort, String order) {
        // page=1&limit=20&userId=1&goodsId=1&sort=add_time&order=desc
        // 对参数值为null的添加默认值
        if (StringUtils.isEmpty(sort)) {
            sort = "add_time";
        }
        if (StringUtils.isEmpty(order)) {
            order = "desc";
        }
        // 对page与limit进行校验，不能为非数字
        Integer pageInt = null;
        Integer limitInt = null;
        try {
            pageInt = Integer.parseInt(page);
            limitInt = Integer.parseInt(limit);
        } catch (Exception e) {
            // 发生错误，返回
            return ResponseUtil.badArgument();
        }
        List<MarketFootprint> marketFootprint = adminUserManagerService.getFootprintList(pageInt, limitInt, userId, goodsId, sort, order);
        Object o = ResponseUtil.okList(marketFootprint);
        // 返回请求
        return o;

    }

    // /admin/collect/list
    @GetMapping("collect/list")
    public Object collectList(String page, String limit, String valueId, String userId, String sort, String order) {
        // page=1&limit=20&userId=1&valueId=1&sort=add_time&order=desc

        // 对参数值为null的添加默认值
        if (StringUtils.isEmpty(sort)) {
            sort = "add_time";
        }
        if (StringUtils.isEmpty(order)) {
            order = "desc";
        }
        // 对page与limit进行校验，不能为非数字
        Integer pageInt = null;
        Integer limitInt = null;
        try {
            pageInt = Integer.parseInt(page);
            limitInt = Integer.parseInt(limit);
        } catch (Exception e) {
            // 发生错误，返回ResponseUtil.badArgument();
            return ResponseUtil.badArgument();
        }
        List<MarketCollect> marketCollect = adminUserManagerService.getCollectList(pageInt, limitInt, userId, valueId, sort, order);
        Object o = ResponseUtil.okList(marketCollect);
        // 返回请求
        return o;
    }

    // /admin/address/list
    @GetMapping("address/list")
    public Object addressList(String page, String limit, String name, String userId, String sort, String order) {
        // page=1&limit=20&name=1&userId=1&sort=add_time&order=desc
        // 对参数值为null的添加默认值
        if (StringUtils.isEmpty(sort)) {
            sort = "add_time";
        }
        if (StringUtils.isEmpty(order)) {
            order = "desc";
        }
        // 对page与limit进行校验，不能为非数字
        Integer pageInt = null;
        Integer limitInt = null;
        try {
            pageInt = Integer.parseInt(page);
            limitInt = Integer.parseInt(limit);
        } catch (Exception e) {
            // 发生错误，返回
            return ResponseUtil.badArgument();
        }

        List<MarketAddress> marketAddress = adminUserManagerService.getAddressList(pageInt, limitInt, name, userId, sort, order);
        Object o = ResponseUtil.okList(marketAddress);
        // 返回请求
        return o;
    }

    // 查找历史记录模块
    /*
    /admin/history/list
        请求参数：
        page=1&limit=20&sort=add_time&order=desc
     */
    @GetMapping("history/list")
    public Object historyList(String page, String limit, String keyword, String userId, String sort, String order) throws IOException {
        // page=1&limit=20&userId=1&keyword=11&sort=add_time&order=desc
        // 对参数值为null的添加默认值
        if (StringUtils.isEmpty(sort)) {
            sort = "add_time";
        }
        if (StringUtils.isEmpty(order)) {
            order = "desc";
        }
        // 对page与limit进行校验，不能为非数字
        Integer pageInt = null;
        Integer limitInt = null;
        try {
            pageInt = Integer.parseInt(page);
            limitInt = Integer.parseInt(limit);
        } catch (Exception e) {
            // 发生错误，返回
            return ResponseUtil.badArgument();
        }
        List<MarketSearchHistory> marketSearchHistories = adminUserManagerService.getSearchHistoryList(pageInt, limitInt, keyword, userId, sort, order);
        Object o = ResponseUtil.okList(marketSearchHistories);
        // 返回请求
        return o;
    }

    // /admin/feedback/list
    @GetMapping("feedback/list")
    public Object feedbackList(String page, String limit, String username, String id, String sort, String order) {
        // page=1&limit=20&username=1&sort=add_time&order=desc&id=11
        // 对参数值为null的添加默认值
        if (StringUtils.isEmpty(sort)) {
            sort = "add_time";
        }
        if (StringUtils.isEmpty(order)) {
            order = "desc";
        }
        // 对page与limit进行校验，不能为非数字
        Integer pageInt = null;
        Integer limitInt = null;
        try {
            pageInt = Integer.parseInt(page);
            limitInt = Integer.parseInt(limit);
        } catch (Exception e) {
            // 发生错误，返回
            return ResponseUtil.badArgument();
        }
        List<MarketFeedback> marketFeedbacks = adminUserManagerService.getMarketFeedbackList(pageInt, limitInt, username, id, sort, order);
        Object o = ResponseUtil.okList(marketFeedbacks);
        // 返回请求
        return o;
    }
}
